home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / UTIL / LOG.CPP < prev    next >
C/C++ Source or Header  |  1994-12-13  |  2KB  |  73 lines

  1. #include "..\au.hpp"
  2.  
  3. /*************************************************************************/
  4. HANDLE *LOG::find(char *log_file)
  5. {
  6.     for (int i=0; i<num_handles; i++)
  7.     {
  8.         if (stricmp(file_name[i], log_file) == 0)
  9.             return handle[i];
  10.     }
  11.     return NULL;
  12. }
  13.  
  14. HANDLE *LOG::add(AU *au, char *log_file)
  15. {
  16.     if (num_handles < MAX_LOGS)
  17.     {
  18.         handle[num_handles]->open(au, log_file,
  19.                                   O_CREAT|O_WRONLY|O_TEXT|O_APPEND);
  20.         file_name[num_handles] = new char[strlen(log_file)+1];
  21.         strcpy(file_name[num_handles], log_file);
  22.         num_handles++;
  23.         return handle[num_handles-1];
  24.     }
  25.     handle[MAX_LOGS-1]->close();
  26.     handle[MAX_LOGS-1]->open(au, log_file,
  27.                              O_CREAT|O_WRONLY|O_TEXT|O_APPEND);
  28.     delete [] file_name[MAX_LOGS-1];
  29.     file_name[MAX_LOGS-1] = new char[strlen(log_file)+1];
  30.     strcpy(file_name[MAX_LOGS-1], log_file);
  31.     return handle[MAX_LOGS-1];
  32. }
  33.  
  34. void LOG::write(AU *au, char *log_file, char *string)
  35. {
  36.     HANDLE *handle;
  37.  
  38.     if (log_file[0] == '\0')
  39.         return;
  40.  
  41.     if ((handle = find(log_file)) == NULL)
  42.     {
  43.         if ((handle = add(au, log_file)) == NULL)
  44.         {
  45.             printf("Unable to log the message: \"%s\" to file %s\n", string, log_file);
  46.             if (!ask_continue())
  47.                 exit(1);
  48.         }
  49.     }
  50.     handle->write_raw(string, strlen(string));
  51.  
  52.     return;
  53. }
  54.  
  55. LOG::LOG()
  56. {
  57.     num_handles = 0;
  58.     for (int i=0; i<MAX_LOGS; i++)
  59.         handle[i] = new HANDLE(0);
  60. }
  61.  
  62. LOG::~LOG()
  63. {
  64.     int i;
  65.  
  66.     for (i=0; i<MAX_LOGS; i++)
  67.         delete handle[i];
  68.  
  69.     for (i=0; i < num_handles; i++)
  70.         delete [] file_name[i];
  71. }
  72.  
  73.